import logging
import sys
import pandas as pd

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def excel_to_txt(monthNo):
    if not isinstance(monthNo, str) or not monthNo.isdigit():
        logging.error('monthNo 必须为数字字符串。')
        return
    
    try:
        chunksize = 10000  # 分批读取的大小
        chunks = []
        
        # 分批读取Excel文件
        for chunk in pd.read_excel(f'会员信息查询{monthNo}.xlsx', header=None, chunksize=chunksize):
            chunks.append(chunk)
        
        df = pd.concat(chunks)
        logging.info('开始写入txt文件')
        
        # 分批写入txt文件
        with open(f'会员信息查询{monthNo}.txt', 'w', encoding='utf-8') as f:
            for chunk in pd.read_excel(f'会员信息查询{monthNo}.xlsx', header=None, chunksize=chunksize):
                chunk.to_csv(f, header=None, sep='\t', index=False, mode='a')
        
        logging.info('文件写入成功!')
    except FileNotFoundError:
        logging.error(f'文件 "会员信息查询{monthNo}.xlsx" 未找到，请检查文件路径。')
    except Exception as e:
        logging.error(f'发生错误: {e}')

if __name__ == '__main__':
    monthNo = sys.argv[1]
    excel_to_txt(monthNo)
